package com.hghivln.erpwarehouse.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.erpwarehouse.business.entity.Goods;
import com.hghivln.erpwarehouse.business.entity.Provider;
import com.hghivln.erpwarehouse.business.mapper.GoodsMapper;
import com.hghivln.erpwarehouse.business.service.GoodsService;
import com.hghivln.erpwarehouse.business.service.ProviderService;
import com.hghivln.erpwarehouse.business.vo.GoodsVo;
import com.hghivln.erpwarehouse.system.common.lang.Constant;
import com.hghivln.erpwarehouse.system.common.lang.DataGridlView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-07-10
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private ProviderService providerService;

    @Override
    public DataGridlView QueryAllGoods(GoodsVo goodsVo) {
        IPage<Goods> page = new Page<>(goodsVo.getPage(),goodsVo.getLimit());
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        qw.like(!StringUtils.isEmpty(goodsVo.getProviderid()),"providerid",goodsVo.getProviderid());
        qw.like(!StringUtils.isEmpty(goodsVo.getGoodsname()),"goodsname",goodsVo.getGoodsname());
        qw.eq(!StringUtils.isEmpty(goodsVo.getProductcode()),"productcode",goodsVo.getProductcode());
        qw.eq(!StringUtils.isEmpty(goodsVo.getPromitcode()),"promitcode",goodsVo.getPromitcode());
        qw.like(!StringUtils.isEmpty(goodsVo.getDescription()),"description",goodsVo.getDescription());
        qw.like(!StringUtils.isEmpty(goodsVo.getSize()),"size",goodsVo.getSize());
        this.getBaseMapper().selectPage(page, qw);
        List<Goods> records = page.getRecords();
        for (Goods goods : records) {
            if (goods.getProviderid() != null) {
                Provider provider = providerService.getById(goods.getProviderid());
                goods.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridlView(page.getTotal(),page.getRecords());
    }

    @CachePut(cacheNames = "com.hghivln.erpwarehouse.business.service.impl.GoodsServiceImpl",key = "#result.id")
    @Override
    public Goods addGoods(Goods goods) {
        getBaseMapper().insert(goods);
        return goods;
    }

    @CachePut(cacheNames = "com.hghivln.erpwarehouse.business.service.impl.GoodsServiceImpl",key = "#result.id")
    @Override
    public Goods updateGoods(Goods goods) {
        Goods selectById = getBaseMapper().selectById(goods.getId());
        BeanUtil.copyProperties(goods,selectById, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        getBaseMapper().updateById(selectById);
        return selectById;
    }

    @Override
    public DataGridlView getAllAvailableGoods() {
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        qw.eq("available", Constant.AVAILABLE_TRUE);
        List<Goods> list = getBaseMapper().selectList(qw);
        return new DataGridlView(list);
    }

    @Override
    public DataGridlView getGoodsByProviderId(Integer id) {
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        qw.eq("available", Constant.AVAILABLE_TRUE);
        qw.eq("providerid",id);
        if (id == null){
            return new DataGridlView();
        }
        List<Goods> goods = getBaseMapper().selectList(qw);
        return new DataGridlView(goods);
    }

    @CacheEvict(cacheNames = "com.hghivln.erpwarehouse.business.service.impl.GoodsServiceImpl",key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }
}
